/*
 * Copyright 2013 Sigurd Randoll <srandoll@digiway.de>.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package de.digiway.rapidbreeze.shared.rest.url;

import de.digiway.rapidbreeze.shared.rest.IdentifiableObject;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlRootElement;
import org.apache.commons.lang3.Validate;

/**
 * This resource contains Information about a specific URL.
 *
 * @author Sigurd Randoll <srandoll@digiway.de>
 */
@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name = "Url")
public class UrlResource implements IdentifiableObject {

    private String filename;
    private Long filesize;
    private Status status;
    private String cloudProvider;
    private String url;

    private UrlResource(Builder builder) {
        this.filename = builder.filename;
        this.filesize = builder.filesize;
        this.status = builder.status;
        this.cloudProvider = builder.cloudProvider;
        this.url = builder.url;
    }

    protected UrlResource() {
    }

    public String getUrl() {
        return url;
    }

    public String getFilename() {
        return filename;
    }

    public Long getFilesize() {
        return filesize;
    }

    public String getCloudProvider() {
        return cloudProvider;
    }

    public String getStatus() {
        return status.toString();
    }

    public boolean isAvailable() {
        return Status.AVAILABLE.equals(status);
    }

    public boolean isNotAvailable() {
        return Status.NOT_AVAILABLE.equals(status);
    }

    @Override
    public String getIdentifier() {
        return getUrl();
    }

    public static class Builder {

        private String filename;
        private Long filesize;
        private Status status;
        private String cloudProvider;
        private String url;

        public Builder filename(String value) {
            this.filename = value;
            return this;
        }

        public Builder filesize(Long value) {
            this.filesize = value;
            return this;
        }

        public Builder ok() {
            this.status = Status.AVAILABLE;
            return this;
        }

        public Builder notAvailable() {
            this.status = Status.NOT_AVAILABLE;
            return this;
        }

        public Builder cloudProvider(String value) {
            this.cloudProvider = value;
            return this;
        }

        public Builder url(String value) {
            this.url = value;
            return this;
        }

        public UrlResource build() {
            Validate.notNull(status, "Status of resource must be set.");
            return new UrlResource(this);
        }
    }

    private static enum Status {

        AVAILABLE, NOT_AVAILABLE;
    }
}
